

/*
 	
 //地图中文注记 http://t3.tianditu.com/DataServer?T=cva_w&x=6579&y=3266&l=13
  
  //地图瓦片 http://t6.tianditu.com/DataServer?T=vec_w&x=6573&y=3265&l=13
  
  //卫星瓦片 http://t0.tianditu.com/DataServer?T=img_w&x=13156&y=6531&l=14
  //卫星标注 http://t3.tianditu.com/DataServer?T=cia_w&x=13156&y=6532&l=14
 */

TdtSwither = function(opt_options) {
	
	this.unSelectStyle = 'box-shadow: rgba(0, 0, 0, 0.35) 2px 2px 3px; border-width: 1px; border-style: solid; border-color: rgb(139, 164, 220); background: rgb(255, 255, 255); padding: 2px 6px; font-style: normal; font-variant: normal; font-stretch: normal; font-size: 12px; line-height: 1.3em; font-family: arial, sans-serif; text-align: center; white-space: nowrap; border-radius: 0px 3px 3px 0px; color: rgb(0, 0, 0);' ;
	this.selectStyle = 'box-shadow: rgba(0, 0, 0, 0.35) 2px 2px 3px;border-left: 1px solid rgb(139, 164, 220);border-top: 1px solid rgb(139, 164, 220);border-bottom: 1px solid rgb(139, 164, 220);background: rgb(46, 73, 134);padding: 2px 6px;font-style: normal;font-variant: normal;font-stretch: normal;font-size: 12px;line-height: 1.3em;font-family: arial, sans-serif;text-align: center;white-space: nowrap;border-radius: 3px 0px 0px 3px;color: rgb(255, 255, 255);font-weight: bold;' ;

	
 	 
  var options = opt_options ? opt_options : {};

  var className = options.className !== undefined ? options.className : '';
 

  var vec_wLabel = '\u5730\u56fe';
  var img_cLabel = '\u6df7\u5408';

  var vec_wTipLabel = '\u5929\u5730\u56fe\u5168\u7403\u77e2\u91cf\u5730\u56fe\u670d\u52a1';
  var img_cTipLabel = '\u5929\u5730\u56fe\u5168\u7403\u5f71\u50cf\u5730\u56fe\u670d\u52a1';

  var vecElement = document.createElement('button');

  this.vecButton = vecElement;
  vecElement.className = className  ;
  vecElement.style = this.selectStyle ;
  vecElement.setAttribute('type', 'button');
  vecElement.title = vec_wTipLabel;
  vecElement.appendChild(
      typeof vec_wLabel === 'string' ? document.createTextNode(vec_wLabel) : vec_wLabel
  );
    

  var imgElement = document.createElement('button');

  this.imgButton = imgElement;
  imgElement.className = className ;
  imgElement.style = this.unSelectStyle ;
  imgElement.setAttribute('type', 'button');
  imgElement.title = img_cTipLabel;
  imgElement.appendChild(
      typeof img_cLabel === 'string' ? document.createTextNode(img_cLabel) : img_cLabel
  ); 
 

  var cssClasses = className /*+ ' ' + ol.css.CLASS_UNSELECTABLE + ' ' +
      ol.css.CLASS_CONTROL*/;
  var element = document.createElement('div');
  element.className = cssClasses;
  element.appendChild(vecElement);
  element.appendChild(imgElement);
   
  element.style = 'bottom: auto; right: 10px; top: 10px; left: auto; white-space: nowrap; cursor: pointer; position: absolute; z-index: 10; text-size-adjust: none;'
 
  var ts = this ; 
   ol.control.Control.call(this, {
	    element: element,
	    target: options.target ,
	    render:function(e){
	    	
	    	if(!ts.load){ 
	    		ts.load = true;
	    		var map = e.map ;
	    		ts.swither_( 'vec' , e.map  )
	    	}
	    	
	    }
	  });
 
   vecElement.onclick = function(){
	   ts. handleClick_('vec');
	};
   imgElement.onclick = function(){
	   ts. handleClick_('img');
	};
 

};
ol.inherits(TdtSwither, ol.control.Control);

 

/** 
 * @param {Event} event The event to handle
 * @private
 */
TdtSwither.prototype.handleClick_ = function( type) {
   
  if(type == 'vec'){
	  this.imgButton.style = this.unSelectStyle; // = imgElement;
	  this.vecButton.style = this.selectStyle;// = vecElement;
  } else if(type == 'img'){
	  this.imgButton.style = this.selectStyle; // = imgElement;
	  this.vecButton.style = this.unSelectStyle;// = vecElement;
  }
  
  this.swither_(type);
  
};
 

var vec_url = 'http://t{0-6}.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}';
var cva_url = 'http://t{0-6}.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}';

var img_url = 'http://t{0-6}.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}';
var cia_url = 'http://t{0-6}.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}';

/**
 * 
 * @param layer vec-地图
 * img-混合包含卫星图
 */ 
TdtSwither.prototype.swither_ = function(layer , _map) {
  var map = _map ? _map : this.getMap();
   
  var currTdt = map.tdtlayer;
  if(currTdt) { 
	  for(var i in currTdt){
		  map.removeLayer(currTdt[i]);
	  }
	 
  }
  
  if(!this[layer]){
	  var l1_url = '', l2_url = '';
	  //create layer
	  if(layer == 'vec'){
		  l1_url = vec_url ;
		  l2_url = cva_url ; 
		  
	  } else if(layer == 'img'){
		  l1_url = img_url ;
		  l2_url = cia_url ;
	  }
	  if(l1_url != '' && l2_url != '') {
		   
		  var layers = [new ol.layer.Tile({
		      source: new ol.source.XYZ({
		        url: l1_url
		      }),
		      zIndex:-2
		    }) ,
		new ol.layer.Tile({
		        source: new ol.source.XYZ({
		          url: l2_url
		        }),
			zIndex:-1
		    })

		];
		  
		 this[layer] = layers ;
	  }
		
  }
  
  if(this[layer]){
	  map.tdtlayer = this[layer];
	  map.addLayer(this[layer][0]);
	  map.addLayer(this[layer][1]);
  }
   
};


TdtSwither.prototype.createLayer = function(_xyz_url , _layer_name  ) {
	  
	var layer =  new ol.layer.Tile({
        source: new ol.source.XYZ({
            url: _xyz_url
          })
        });
	layer.layerName = _layer_name;
	
	return layer ;
};

